package com.cysoft.dao;

import com.cysoft.domain.FilterType;
import com.cysoft.domain.Mold;
import com.cysoft.domain.Wheel;
import com.cysoft.domain.WheelTechType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
@Component
public class WheelDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private MoldDao moldDao;

    public List<Wheel> getWheelListFromDB() {


        String sql = "SELECT w.* ,d.devie_run_cost_per_hour as cost_time FROM wheel w INNER JOIN  device_capacity d ON w.wheel_code=d.wheel_code AND w.filter_type is not null";

        List<Wheel> wheelList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Wheel>(Wheel.class){
            @Override
            public Wheel mapRow(ResultSet rs, int rowNumber) throws SQLException {
                Wheel wheel = new Wheel(rs.getInt("id"),rs.getString("wheel_code"),rs.getString("wheel_name"),rs.getInt("cost_time"));

                WheelTechType techType = WheelTechType.fromStr(rs.getString("wheel_tech_type"));
                wheel.setWheelTechType(techType);

                List<FilterType> typeList = new ArrayList<>();
                int filterType = rs.getInt("filter_type");
                switch (filterType) {
                    case 0:
                        typeList.add(FilterType.XIANWEI);
                        break;
                    case 1:
                        typeList.add(FilterType.TIEWANG);
                        break;
                    case 2:
                        typeList.add(FilterType.XIANWEI);
                        typeList.add(FilterType.TIEWANG);
                        break;
                }
                wheel.setFilterTypeList(typeList);
                List<Mold> moldList = moldDao.getMoldListByWheelCode(wheel.getWheelCode());
                wheel.setMoldList(moldList);

                return wheel;
            }
        });

        return wheelList;
    }



    public Wheel getWheelByCode(String code) {


        String sql = "SELECT w.* ,d.devie_run_cost_per_hour as cost_time FROM wheel w INNER JOIN  device_capacity d ON w.wheel_code=d.wheel_code AND w.filter_type is not null and w.wheel_code = '"+code+"'";

        List<Wheel> wheelList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Wheel>(Wheel.class){
            @Override
            public Wheel mapRow(ResultSet rs, int rowNumber) throws SQLException {
                float costTime = 14;
                if(rs.getFloat("cost_time") != 0 ) {
                    costTime = 60.0f / rs.getFloat("cost_time");
                }
                Wheel wheel = new Wheel(rs.getInt("id"),rs.getString("wheel_code"),rs.getString("wheel_name"),costTime);

                WheelTechType techType = WheelTechType.fromStr(rs.getString("wheel_tech_type"));
                wheel.setWheelTechType(techType);

                List<FilterType> typeList = new ArrayList<>();
                int filterType = rs.getInt("filter_type");
                switch (filterType) {
                    case 0:
                        typeList.add(FilterType.XIANWEI);
                        break;
                    case 1:
                        typeList.add(FilterType.TIEWANG);
                        break;
                    case 2:
                        typeList.add(FilterType.XIANWEI);
                        typeList.add(FilterType.TIEWANG);
                        break;
                }
                wheel.setFilterTypeList(typeList);
                List<Mold> moldList = moldDao.getMoldListByWheelCode(wheel.getWheelCode());
                wheel.setMoldList(moldList);

                return wheel;
            }
        });

        if(wheelList.size() > 0) {
            return wheelList.get(0);
        }
        return null;
    }



}
